package com.qi.xiaogangzai.pccache;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;

import com.jakewharton.rxbinding2.view.RxView;
import com.qi.xiaogangzai.pccache.imgloader.RxImageLoader;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * RxJava实现三级缓存框架
 * 博客地址
 */
public class MainActivity extends AppCompatActivity {
    private Button mButton;
    private ImageView mImageView;
    private String murl = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.main_show_pc_btn);
        mImageView = (ImageView) findViewById(R.id.main_show_content_iv);
        murl = "http://img2.baa.bitautotech.com/usergroup/editor_pic/2017/3/22/694494c2f3544226ae911bf86b4e2bcc.png";
        //RxImageLoader.with(this).load("").into(null);
        //创建被观察者
       /* final Observable<String> memoryObservable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("");
                e.onComplete();
            }
        });

        final Observable<String> diskObservable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("disk");
                e.onComplete();
            }
        });

        final Observable<String> networkObservable = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("network");
                e.onComplete();
            }
        });
        RxView.clicks(mButton).subscribe(new Consumer<Object>() {
            @Override
            public void accept(@NonNull Object o) throws Exception {
                Observable.concat(memoryObservable, diskObservable, networkObservable)
                        *//*.first(new Predicate<String>() {
                            @Override
                            public boolean test(@NonNull String s) throws Exception {
                                return TextUtils.isEmpty(s);
                            }
                        })*//*
                        .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(@NonNull String s) throws Exception {
                        Log.d("MainActivity","get data from"+s);
                    }
                });
            }
        });*/
        RxView.clicks(mButton).subscribe(new Consumer<Object>() {
            @Override
            public void accept(@NonNull Object o) throws Exception {
                RxImageLoader.with(MainActivity.this).load(murl).into(mImageView);
            }
        });
    }
}
